package com.ymttest.database.sqlwapper.sqidcard;
import java.util.HashMap;
import java.util.List;
import java.util.Optional;

import com.ymt.database.SqlManager;
import com.ymttest.database.model.sqidcard.IdcardInfo;

public class IdcardInfoWrapper {
	
	SqlManager manager;

	public IdcardInfoWrapper() {
		manager = new SqlManager("sqidcard");
	}

	@SuppressWarnings("unchecked")
	public IdcardInfo selectByPrimaryKey(Long id) {

		IdcardInfo idcardInfo = null;
		List<IdcardInfo> result = manager
				.getSelectResults(
						"com.ymttest.database.mapping.sqidcard.IdcardInfoMapper.selectByPrimaryKey",
						id);
		manager.commit();

		if (result.size() > 0) {
			idcardInfo = result.get(0);
		}

		return idcardInfo;
	}
	
	@SuppressWarnings("unchecked")
	public IdcardInfo selectByIdcardIdAndUserId(Long idcardId, Long userId) {

		IdcardInfo idcardInfo = new IdcardInfo();
		
		HashMap<String, Long> map = new HashMap<>();
		
		map.put("idcardId", idcardId);
		map.put("userId", userId);
		
		List<IdcardInfo> result = manager
				.getSelectResults(
						"com.ymttest.database.mapping.sqidcard.IdcardInfoMapper.selectByIdcardIdAndUserId",
						map);
		manager.commit();

		if (result.size() > 0) {
			idcardInfo = result.get(0);
		}

		return idcardInfo;
	}
	
	@SuppressWarnings("unchecked")
	public IdcardInfo selectByNameAndNoAndUserId(String name, String idcardNo, Long userId) {

		IdcardInfo idcardInfo = new IdcardInfo();
		
		HashMap<String, Object> map = new HashMap<>();
		
		map.put("idcardName", name);
		map.put("idcardNo", idcardNo);
		map.put("userId", userId);
		
		List<IdcardInfo> result = manager
				.getSelectResults(
						"com.ymttest.database.mapping.sqidcard.IdcardInfoMapper.selectByNameAndNoAndUserId",
						map);
		manager.commit();

		if (result.size() > 0) {
			idcardInfo = result.get(0);
		}

		return idcardInfo;
	}
	
	@SuppressWarnings("unchecked")
	public IdcardInfo selectLastestIdcardInfo(Long userId) {

		IdcardInfo idcardInfo = null;
		
		List<IdcardInfo> result = manager
				.getSelectResults(
						"com.ymttest.database.mapping.sqidcard.IdcardInfoMapper.selectLastestIdcardInfo",
						userId);
		manager.commit();

		if (result.size() > 0) {
			
			Optional<IdcardInfo> op = result.stream().filter(info -> info.getPush_status() == true).findFirst();
			
			if (op.isPresent())
				idcardInfo = op.get();
			else
				idcardInfo = result.stream().filter(info -> info.getPush_status() == false).findFirst().get();
				
		}

		return idcardInfo;
	}
	
	public int deleteIdcardInfoByUserId(Long userId) {

		int result = manager
				.delete("com.ymttest.database.mapping.sqidcard.IdcardInfoMapper.deleteIdcardInfoByUserId",
						userId);

		manager.commit();

		return result;
	}
	
	public static void main(String[] args) {
		
		IdcardInfoWrapper test = new IdcardInfoWrapper();
		
//		SellerLevelHistory sellerLevelHistory = new SellerLevelHistory();
//		
//		sellerLevelHistory.setDDate(YMTDateUtil.getDate());
//		sellerLevelHistory.setSellerID(24635724);
//		sellerLevelHistory.setUpdateTime(YMTDateUtil.getDate());
//		sellerLevelHistory.setUnconfirm_amount(BigDecimal.TEN);
//		
		System.out.println(test.selectLastestIdcardInfo(88149400000L).getIdcard_name());
	}
}
